<?php
/**
 * Created by PhpStorm.
 * User: XZR
 * Date: 2018/8/1
 * Time: 15:21
 */

namespace Api\Controller;


//投票控制器

use Api\Model\UserVoteModel;
use Api\Model\VoteOptionModel;

class VoteController extends AuthController
{

    //投票操作
    public function index(){
        $this->validateParam(array("record_id",'rate'));
        $record_id = $this->getPostParam('record_id');
        $rate = $this->getPostParam('rate');
        //判断能否投票
        $row = UserVoteModel::singleton()->getByUserIdAndRecordId($this->userId,$record_id);
        if(!$row){
            $id = UserVoteModel::singleton()->vote($this->userId,$record_id,$rate);
            if($id){
                //投票完访问当前比例
                $rst = array();
                $rst['result']=200;
                $rst['info']['avg_rate']=UserVoteModel::singleton()->getTotalRateByRecordId($record_id);;
                $rst['msg']='提交成功！';
            }else{
                $rst = array();
                $rst['result']=202;
                $rst['msg']='系统繁忙！';
            }
        }else{
            $rst = array();
            $rst['result']=201;
            $rst['msg']='已经提交过！';
        }
        $this->formatjson($rst);
    }


    public function test(){
        $data=[
            'commend_status'=>0,
            'name'=>"程耀国 教授 北京大学第三医院",
            'id'=>'181126-001',
            'age'=>33,
            'sex'=>'男',
            'clear'=>'3年',
            'glass'=>'2.5年',
            'rate'=>20.45,
        ];
        header("content-type:image/jpeg");
        if($data['commend_status']){
            $img = imagecreatefromjpeg('./Public/commend.jpg');
        }else{
            $img = imagecreatefromjpeg('./Public/uncommend.jpg');
        }

        //$font ='./Public/msyh.ttc';               // 设置字体
        $font='C:\phpStudy\PHPTutorial\WWW\dmk\Public\msyh.ttc';
        $black = imagecolorallocate($img, 0x06, 0x06, 0x06);  // 颜色
        $grey = imagecolorallocate($img, 0xef, 0xf2, 0xf4);  // 颜色

        imagettftext($img, 20, 0, 68, 110, $black, $font,$data['name'] );       // 写入name

        imagettftext($img, 18, 0, 150, 194, $black, $font,$data['id'] );       // 写入id
        imagettftext($img, 18, 0, 395, 194, $black, $font,$data['age'] );       // 写入age
        imagettftext($img, 18, 0, 600, 194, $black, $font,$data['sex'] );       // 写入sex
        imagettftext($img, 18, 0, 130, 242, $black, $font,$data['clear']  );       // 写入clear
        imagettftext($img, 18, 0, 400, 242, $black, $font,$data['glass']  );       // 写入glass

        $bule = imagecolorallocate($img,0x27,0xbb,0xff);//创建一个颜色，以供使用
        imagettftext($img, 25, 0, 520, 495, $bule, $font,$data['rate']."%"  );       // 写入glass
        imagettftext($img, 25, 0, 521, 496, $bule, $font,$data['rate']."%"  );

        imagejpeg($img);     // 生成jpeg格式图片
        imagedestroy($img);  // 销毁图片*/
    }

}